package com.example.plat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.plat.enity.Activity;
import com.example.plat.enity.Place;
import com.example.plat.exception.ActivityException;
import com.example.plat.mapper.ActivityMapper;
import com.example.plat.service.ActivityService;
import com.example.plat.util.ImageHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper mapper;


    @Override
    public boolean isJoin(Long userId,Long actId){
        Integer join = mapper.isJoin(userId, actId);
        return join>0?true:false;
    }

    @Transactional
    @Override
    public Activity createActivity(Activity activity, List<String> images) throws ActivityException {
        try {
            mapper.insert(activity);
        }catch (Exception e){
            throw new ActivityException("发布活动失败！该活动名已被使用！");
        }
        try {
            for(String i:images){
                mapper.uploadImage(activity.getId(),i);
            }
        }catch (Exception e){
            throw new ActivityException("图片上传失败，发表活动失败！");
        }

        return activity;
    }

    @Transactional
    @Override
    public String joinActivity(Long userId, Long actId)throws ActivityException{
        try {
            if(isJoin(userId,actId)){
                return "已参与该活动！";
            }
            mapper.joinActivity(userId, actId);
            mapper.updateParticipation(actId);
        }catch (Exception e){
            throw new ActivityException("参与活动失败！");
        }
        return "参与活动成功！";
    }

    @Transactional
    @Override
    public void updateActivity(Activity activity, List<String> images) throws ActivityException{
        try {
            mapper.updateActivity(activity.getTitle(),activity.getCategory(),activity.getContent(),
                    activity.getPlace(),activity.getPlaceId(),activity.getTime(),activity.getNumOfPerson(),
                    activity.getPhoto(),activity.getId());
        }catch (Exception e){
            throw new ActivityException("更新活动失败");
        }
        try {
            for(String i:images){
                mapper.deleteImage(activity.getId());
                mapper.uploadImage(activity.getId(),i);
            }
        }catch (Exception e){
            throw new ActivityException("图片上传失败，修改活动失败！");
        }
    }

    @Transactional
    @Override
    public void deleteActivity(Long actId) throws ActivityException{
        try{
            mapper.deleteById(actId);
        }catch (Exception e){
            throw new ActivityException("删除活动失败");
        }
    }

    @Transactional
    @Override
    public void cancelJoin(Long userId, Long actId) throws ActivityException {
        try {
            mapper.cancelJoin(userId,actId);
            mapper.cancepParticipation(actId);
        }catch (Exception e){
            throw new ActivityException("取消参与活动失败！");
        }
    }

    @Override
    public void collect(Long userId, Long actId) throws ActivityException {
        try {
            mapper.collect(userId,actId);
        }catch (Exception e){
            throw new ActivityException("收藏活动失败！");
        }
    }

    @Override
    public void cancleCollect(Long userId, Long actId) throws ActivityException {
        try{
            mapper.cancleCollect(userId,actId);
        }catch (Exception e){
            throw new ActivityException("取消收藏活动失败！");
        }
    }


}
